package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.domain.Course;
import com.example.evanproject.domain.Coursefeedback;
import com.example.evanproject.domain.Register;
import com.example.evanproject.mapper.CourseMapper;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.service.CoursefeedbackService;
import com.example.evanproject.mapper.CoursefeedbackMapper;
import com.example.evanproject.util.ReturnsData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author hanxiaoqi
* @description 针对表【coursefeedback】的数据库操作Service实现
* @createDate 2024-03-09 21:52:12
*/
@Service
public class CoursefeedbackServiceImpl extends ServiceImpl<CoursefeedbackMapper, Coursefeedback>
    implements CoursefeedbackService{
    @Autowired
    private CoursefeedbackMapper courseFeedbackMapper;
    public ReturnsData createfeed(Integer cId, Integer sId, Integer start, String content) {

        //获取当前时间带有时分秒
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= dateFormat.format(date);
        System.out.println(time);

    ReturnsData returnsData = new ReturnsData();
        if(courseFeedbackMapper.createfeed(cId,sId,start,content,time)>0){
            returnsData.setCode(200);
            returnsData.setMsg("成功");
            returnsData.setData("1");

        }else {
            returnsData.setCode(400);
            returnsData.setMsg("失败");
        }

        return returnsData;
    }


    //获取课程的评价 再次获取评价人的信息 将两者返回去
    public ReturnsData selectallfeed(Integer cid){
      ReturnsData returnsData=new ReturnsData();
        returnsData.setCode(200);
        returnsData.setMsg("成功");
        returnsData.setData(courseFeedbackMapper.selectallfeed(cid));
        return returnsData;


    }

    @Autowired
    private RegisterMapper registerMapper;
    public Map<String,Object> selectallfeedtext1(Integer cid){

        List<Coursefeedback> list =courseFeedbackMapper.selectallfeed(cid);
//        查找符合范围的用户
        //去重 因为是范围筛选
        if(list.size()>0){
            Set set = new HashSet();
            for(Integer i=0;i<list.size();i++){
                set.add(list.get(i).getPeopleId());
            }
            //将集合转为数组类型
            Integer[] array = (Integer[]) set.toArray(new Integer[set.size()]);
            List<Register> registers=registerMapper.morepople(array);
///////////////////////////////////////////////
//
            System.out.println(registers);
            System.out.println(list);
            List<List> newlist =new ArrayList<>();
            Double start=0.0;
            int sumnum=list.size();
            for (Coursefeedback item:list) {
                start+=item.getCfStart();
                List<Object> list4=new ArrayList<>();
                list4.add(item);
                for(int i=0;i<registers.size();i++){
                    if( item.getPeopleId()==registers.get(i).getSId()){
                        list4.add(registers.get(i));
                    }
                }
                newlist.add(list4);
            }
            //计算分数
            start=(start+0.0)/sumnum;

            Map<String,Object> map1=new HashMap<>();
            map1.put("list",newlist);
            map1.put("sumnum",sumnum);
            map1.put("start",start);
            System.out.println(newlist);
            return map1;
        }else {
            Map<String,Object> map=new HashMap<>();
            return map;
        }

    }


    //查看某个用户的评价(返回评价列表老师信息，返回课程信息，返回星级信息)
    @Autowired
    private CourseMapper courseMapper;
    public List<Map> selectByuserid(Integer userid) {
//        封装返回回评价列表老师信息，返回课程信息，返回星级信息
        List<Coursefeedback> list1=courseFeedbackMapper.selectByuserid(userid);
        //当sql语句 是in的话那么是空数组会报错 所以 如果查询出来的list1(人没有任何评价课程的信息) 就返回一个空的list
        if(list1.size()>0){
            //将课程设为数组 方便后面把课程查询出来
            List<String> list2=new ArrayList<>();
            for (int i=0;i<list1.size();i++){
                list2.add(String.valueOf(list1.get(i).getCourseId()));
            }

            String[] ids=list2.toArray(new String[list2.size()]);
            List<Course> list3=courseMapper.selectbycoueseid(ids);
            List<Map> list =new ArrayList<>();
            for (int i=0;i<list1.size();i++){
                Map<String,Object> map=new HashMap<>();
                map.put("str",list1.get(i));
                for(int j=0;j<list3.size();j++){
                    if(list1.get(i).getCourseId()==list3.get(j).getCourseId()){
                        map.put("str2",list3.get(j));
                    }
                }
                list.add(map);
            }
            System.out.println(list);
            return list;
        }
        else {
            List<Map>  listnull=new ArrayList<>();
            return listnull;
        }

    }
    public Integer deletefeedinfo(Integer feedId) {
        return courseFeedbackMapper.deletefeedinfo(feedId);
    }



}




